
package com.zsrt.book.dao.impl;

import java.io.File;
import java.lang.Character.Subset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.print.attribute.standard.RequestingUserName;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jaxen.function.SumFunction;

import com.zsrt.book.dao.UserBookDao;
import com.zsrt.book.util.CommonUtil;

public class UserBookDaoImpl implements UserBookDao{

	/**
	 * 员工借书操作
	 */
	@Override
	public boolean borrowBook(Scanner scanner, String bookConstants,String loginNum,String employeeConstants)  {
		
		//得出文件
		String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		File f = new File(file);
		try {
			document = reader.read(f);
			Element root = document.getRootElement();
			
			List<Element> nodes = root.selectNodes("//book");
			List<Element> books= new ArrayList<Element>();
			System.out.println("可以借阅的图书为：");
			//遍历节点 
			for(Element element:nodes){
				//得到图书借阅状态
				String state= element.element("borrowState").getText();
				//如果可借阅就打印
				if(state.equals("Y")){
					books.add(element);
					System.out.println("图书编号："+element.elementText("bookNum")+"  图书名称："+element.elementText("bookName")+
							"  图书简介："+element.elementText("description")+"   图书借阅状态："+element.elementText("borrowState"));
				}
			}
			
			System.out.println("请输入你想借的图书编号：");
			int num= scanner.nextInt();
			//修改图书信息 
			for(Element e:books){
				//如果编号存在
				if(num==Integer.parseInt(e.elementText("bookNum"))){
					System.out.println("借阅成功");
					Element bElement = e.element("borrowState");
					//将图书改为不可借阅
					bElement.setText("N");
					break;
				}
			}
			try {
				CommonUtil.add(document, file);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			//修改员工的借阅信息
			UpdateEmployee(loginNum, employeeConstants,num);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return true;
	}

	/**
	 * 结束后修改员工信息
	 * @param loginNum
	 * @param employeeConstants
	 * @param num    图书编号
	 * @throws DocumentException
	 */
	private static  void UpdateEmployee(String loginNum, String employeeConstants,int num)
			throws DocumentException {
		System.out.println("修改员工信息成功");
		//修改员工借书信息
		String file1 = employeeConstants;
		SAXReader reader1 = new SAXReader();
		Document  document1 =null;
		File f1 = new File(file1);
		document1 = reader1.read(f1);
		Element root1 = document1.getRootElement();
		
		List<Element> employees= root1.selectNodes("//employee");
		//遍历节点
		for(Element em:employees){
			//找到登录的用户的信息
			if(em.element("cardNum").getText().equals(loginNum)){
				Element borrowBookInfoEmlement= em.element("borrowBookInfo");
				//得到用户借阅图书信息
				String string = borrowBookInfoEmlement.getText();
				//增加员工借书的信息  如果为空就不用加减号
				if(string==null){
					borrowBookInfoEmlement.setText(""+num);
				}else{
					borrowBookInfoEmlement.setText(string+"-"+num);
				}
			}
		}
		
		try {
			CommonUtil.add(document1, file1);
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}

	/**
	 * 员工还书操作
	 */
	@Override
	public boolean revertBook(Scanner scanner, String bookConstants, String loginNum, String employeeConstants) {

		String[] subs = null;
		String file = employeeConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		File f = new File(file);
		try {
			document = reader.read(f);
			Element  root = document.getRootElement();
			List<Element> nodes = root.selectNodes("//employee");
			for(Element element:nodes){
				//找到该员工的信息
				if(loginNum.equals(element.element("cardNum").getText())){
					String str = element.elementText("borrowBookInfo");
					//将借阅图书放在字符串数组中
					if(str==null){
						//借阅图书为空 不用分隔符
						subs=null;
					}else{
						//得到借阅图书的各个编号
						subs= str.split("\\-");
					}
					break;
				}
			}
		}catch (DocumentException e) {
			e.printStackTrace();
		}
		/*for(int index=0;index<subs.length;index++){
			System.out.println(subs[index]);
		}*/
		System.out.println("您借阅的图书是：");
		
		
		String file1 = bookConstants;
		SAXReader reader1 = new SAXReader();
		Document  document1 =null;
		File f1 = new File(file1);
		
		try {
			document1 = reader1.read(f1);
			Element root1 = document1.getRootElement();
			
			List<Element> nodes1 = root1.selectNodes("//book");
			//找到借书信息打印  遍历节点找到您所借阅的所有节点
			for(int a=0;a<subs.length;a++){
				for(Element e:nodes1){
					if(subs[a].equals(e.elementText("bookNum"))){
						System.out.println("图书编号："+e.elementText("bookNum")+"  图书名称："+e.elementText("bookName")+
								"  图书简介："+e.elementText("description"));
					}
				}
			}
			
			System.out.println("请输入您要归还的图书编号");
			String revertNum = scanner.next();
			//归还图书改变图书状态
			for(Element e:nodes1){
				if(revertNum.equals(e.elementText("bookNum"))){
					Element bookElement = e.element("borrowState");
					bookElement.setText("Y");
				}
			}
			//归还图书改变用户借阅图书的状态
			revertBookUpdateEm(loginNum, employeeConstants, revertNum, subs);
			try {
				CommonUtil.add(document1, file1);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}catch (DocumentException e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 员工还书后修改操作
	 * @param loginNum
	 * @param employeeConstants
	 * @param revertNum
	 * @param subs
	 */
	private void revertBookUpdateEm(String loginNum, String employeeConstants,String revertNum,String[] subs) {
		System.out.println("修改员工信息成功");
		//修改员工借书信息
		String file1 = employeeConstants;
		SAXReader reader1 = new SAXReader();
		Document  document1 =null;
		File f1 = new File(file1);
		try {
			document1 = reader1.read(f1);
			Element root1 = document1.getRootElement();
			
			List<Element> employees= root1.selectNodes("//employee");
			//遍历节点找到用户的信息
			for(Element em:employees){
				if(em.element("cardNum").getText().equals(loginNum)){
					//得到用户的状态信息
					Element borrowBookInfoEmlement= em.element("borrowBookInfo");
					
					for(int index=0;index<subs.length;index++){
						//找到要删除的下标
						if(!subs[index].equals(revertNum)){
							
							//修改员工借书的信息
							borrowBookInfoEmlement.setText(subs[index]+"-");
						}
					}
					
					String string= borrowBookInfoEmlement.getText();
					char[] cs = string.toCharArray();
					if(cs[0]=='-' ){
						string=string.substring(1, string.length());
					}else if(cs[cs.length-1] == '-'){
						string=string.substring(0,string.length()-1);
					}else if(cs[0]=='-'   && cs[cs.length-1] == '-'){
						string = string.substring(1,string.length()-1);
					}else{
						string=string;
					}
					borrowBookInfoEmlement.setText(string);
					break;
				}
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		
		try {
			CommonUtil.add(document1, file1);
			
			
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	
}
